package cn.wjj.org.service.impl;

import cn.wjj.basic.ibaseservice.impl.BaseServiceImpl;
import cn.wjj.basic.util.PageList;
import cn.wjj.org.domain.Department;
import cn.wjj.org.mapper.DepartmentMapper;
import cn.wjj.org.query.DepartmentQuery;
import cn.wjj.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void add(Department department) {

        if (department.getParent() == null || department.getParent().getId() == null){
            department.setDirPath("/" + department.getId());
        }else{
            Long id = department.getParent().getId();
            Department parentDept = departmentMapper.findByid(id);
            String dirPath = parentDept.getDirPath();
            department.setDirPath(dirPath + "/" + department.getId());
        }
        departmentMapper.save(department);
    }

    @Override
    public void update(Department department) {
        if (department.getParent() == null || department.getParent().getId() == null){
            department.setDirPath("/" + department.getId());
        }else{
            Long id = department.getParent().getId();//上级部门id
            Department parentDept = departmentMapper.findByid(id);
            //上级部门的dirPath
            String dirPath = parentDept.getDirPath();
            //设置主机的dirpath
            department.setDirPath(dirPath + "/" + department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    public List<Department> gerDeptTree() {
        return departmentMapper.gerDeptTree();
    }
}
